<template>
	<view>
		<view>
			<view class="toolbar">
				<button  @click="reset">重新开始</button>
				<view>
					<ul class="ul1">
					         <li class="li1">
					         			最快成绩：<text style="color: #5555ff;">{{fast}}</text>秒 <br/>  
										开始计时：<text style="color: #ff557f;">{{ctime}}</text>秒
					         </li> 
					</ul>
					
						
				</view>
			</view>
			<!-- <image mode="widthFix" :src="path"/> -->
			<uni-section title="玩法说明:点击空白格子上下左右的格子和空白格子调换位置,最终形成一张完整的图片" type="line">
				<uni-grid :column="3" :show-border="false" :square="true" @change="change">
					<uni-grid-item class="uni-grid-item1" v-for="(item ,index) in list" :index="index" :key="index">
						<view>
							<image :style="{ width: width1 +'px', height: height1 +'px' }" :src="item.url"
								mode="aspectFit" />
						</view>
					</uni-grid-item>
				</uni-grid>
			</uni-section>

			最终效果：
			<image :src="url2" style="width: 100px;height: 100px;margin-top: 10rpx;"></image>
		</view>
	</view>
</template>
<script>
	var jishi_interval;
	var y = true;
	var _self;
	export default {
		onShareAppMessage(res) {
			return {
				title: '拼图',
				path: 'pages/pintu/pintu'
			}
		},
		onShareTimeline() {
			return {
				title: '拼图'
			}
		},
		data() {
			return {
				fast:'',
				ctime:0,
				width1: '106',
				height1: '106',
				url2: "/static/quan.jpg", //原图
				list: [{
						url: '/static/c1.png',
						text: '1'
					},
					{
						url: '/static/c2.png',
						text: '2'
					},
					{
						url: '/static/c3.png',
						text: '3'
					},
					{
						url: '/static/c4.png',
						text: '4'
					},
					{
						url: '/static/c5.png',
						text: '5'
					},
					{
						url: '/static/c6.png',
						text: '6'
					},
					{
						url: '/static/c7.png',
						text: '7'
					},
					{
						url: '/static/c8.png',
						text: '8'
					},
					{
						url: '',
						text: '9'
					}

				]
			}
		},
		onLoad(e) {
			console.log("0==''");
			console.log(0=='');
			_self = this;
			this.random(); //打乱
			uni.getStorage({
				key: "fast",
				success: function(res) {
					_self.fast = res.data;
				}
			});
		},
		onUnload() {

		},
		methods: {
			//点击方格
			change(e) {
				let {
					index
				} = e.detail
				
				if (this.isWin()) {
					uni.showModal({
						content: '拼图已完成！！！请重新开始',
						showCancel: false
					});
					return false;
				}

				let num = this.ck1(index);
				if (num != -1) {
					//交换2个方格的位置
					this.list[num].url = this.list[index].url;
					this.list[num].text = this.list[index].text;
					this.list[index].url = '';
					this.list[index].text = '';
				}
				//判断当局走第一步，开始计时
				if(y){
					this.start_jishi();
					y = false;
				}
				if (this.isWin()) {
					this.end_jishi();
					uni.showModal({
						content: '拼图已完成！！！',
						showCancel: false
					});
					//如果缓存没有成成绩，或者当前成绩好于缓存成绩，则新成绩覆盖缓存成绩
					if(this.fast==='' || this.fast>this.ctime){
						this.fast = this.ctime;
						this.setStorage("fast",this.ctime)
					}
				}
			},
			isWin() {
				//走完一步后判断是否赢了
				let temp = true;
				for (var i = 1; i < this.list.length; i++) {
					if (this.list[i - 1].text != i) {
						temp = false;
					}
				}
				if (temp) {
					return true;
				} else {
					return false;
				}
			},
			setStorage(key, value) { //设置本地缓存
				uni.setStorage({
					key: key,
					data: value,
					success: function() {
					}
				});
			},
			ck1(index) {
				//验证是否能移动，如果能移动，返回可移动的下标，不能移动则返回空字符串
				if (index == 0) {
					if (this.list[1].url == '') {
						return 1;
					} else if (this.list[3].url == '') {
						return 3;
					}
				} else if (index == 1) {
					if (this.list[0].url == '') {
						return 0;
					} else if (this.list[2].url == '') {
						return 2;
					} else if (this.list[4].url == '') {
						return 4;
					}
				} else if (index == 2) {
					if (this.list[1].url == '') {
						return 1;
					} else if (this.list[5].url == '') {
						return 5;
					}
				} else if (index == 3) {
					if (this.list[0].url == '') {
						return 0;
					} else if (this.list[4].url == '') {
						return 4;
					} else if (this.list[6].url == '') {
						return 6;
					}
				} else if (index == 4) {
					if (this.list[1].url == '') {
						return 1;
					} else if (this.list[3].url == '') {
						return 3;
					} else if (this.list[5].url == '') {
						return 5;
					} else if (this.list[7].url == '') {
						return 7;
					}
				} else if (index == 5) {
					if (this.list[2].url == '') {
						return 2;
					} else if (this.list[4].url == '') {
						return 4;
					} else if (this.list[8].url == '') {
						return 8;
					}
				} else if (index == 6) {
					if (this.list[3].url == '') {
						return 3;
					} else if (this.list[7].url == '') {
						return 7;
					}
				} else if (index == 7) {
					if (this.list[4].url == '') {
						return 4;
					} else if (this.list[6].url == '') {
						return 6;
					} else if (this.list[8].url == '') {
						return 8;
					}
				} else if (index == 8) {
					if (this.list[5].url == '') {
						return 5;
					} else if (this.list[7].url == '') {
						return 7;
					}
				}
				return -1;
			},
			random() {
				var num = 0;
				for (var i = 0; i < 1000; i++) {
					var round = Math.round(Math.random() * 10);
					var index = round % 9;
					let num = this.ck1(index);
					if (num != -1) {
						//交换2个方格的位置
						this.list[num].url = this.list[index].url;
						this.list[num].text = this.list[index].text;
						this.list[index].url = '';
						this.list[index].text = '';
					}
				}
			},
			
			reset() {
				this.end_jishi();
				this.random(); //打乱
				this.ctime = 0;
				y = true;
			},
			start_jishi() { //开始计时
				jishi_interval = setInterval(function() {
					_self.ctime = _self.ctime+1;
				}, 1000);
			},
			end_jishi() { //停止计时
				if (jishi_interval) {
					clearInterval(jishi_interval);
				}
			}
		}
	}
</script>
<style>
	.toolbar {
		display: flex;
	}
	ul {
	    padding-top: 1rem;
	    margin: 0;
	    display: flex;
	  }
	.li1 {
		margin:0;
		padding:0;
		flex: 0 0 100%;
		height: 0rem;
		text-align: left;
	}
	.ul1{
		padding-top: 10px;
	}
</style>
